<?php

/* 
 * 友情链接
 */
namespace Admin\Controller;

class FriendLinkController extends AdminController{
    
    public function index(){
        $where = [];
        
        $count = M('FriendLink')->where($where)->count();
        
        $Page = new \Think\Page($count);
        
        $data = M('FriendLink')->where($where)->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();
        
        $this->assign(array(
            'data'  =>  $data,
            'page'  =>  $Page->show(),
                ));
        $this->display();
    }
    
    public function add(){
        
        $Model = M('FriendLink');
        if(IS_POST){
            
            if($data = $Model->create()){
                
                if(empty($data['site_name'])){
                    $this->error('站点名称不能空');
                }
                
                if(empty($data['site_url'])){
                    $data['site_url'] = '#';
                }
                
                if(substr($data['site_url'], 0, 7) != 'http://' && trim($data['site_url']) != '#'){
                    $data['site_url'] = 'http://'.$data['site_url'];
                }
                
                $has = $Model->where(array('site_name'=>$data['site_name']))->count();
                if($has > 0){
                    $this->error('该站点已存在');
                }
                
                if($Model->data($data)->add() !== false){
                    $this->success('添加成功',U('index'));
                }
                
                $this->error('添加失败');
            }
        }else{
            
            $this->display();
        }
    }
    
    public function edit(){
        $Model = M('FriendLink');
        $data = array();
        if(IS_POST){
            $id = I('post.id',0,'intval');
            if(empty($id)){
                $this->error('参数错误');                
            }
            
            if($data = $Model->create()){
                if(empty($data['site_name'])){
                    $this->error('站点名称不能空');
                }
                
                if(empty($data['site_url'])){
                    $data['site_url'] = '#';
                }
                
                $condition['site_name']=$data['site_name'];
                $condition['id'] = array('neq', $id);
                $has = $Model->where($condition)->count();
                if($has > 0){
                    $this->error('该站点已存在');
                }
                
                if(substr($data['site_url'], 0, 7) != 'http://' && trim($data['site_url']) != '#'){
                    $data['site_url'] = 'http://'.$data['site_url'];
                }
                
                if($Model->data($data)->save() !== false){
                    $this->success('链接更新成功');
                }
                
                $this->error('链接更新失败');                
            }
        }else{
            $id = I('get.id',0,'intval');
            if(empty($id)){
                $this->error('参数错误');
            }
            
            $data = $Model->find($id);
            if(empty($data)){
                $this->error('没有找到该链接');
            }
            
            $this->assign('data', $data);
            $this->display();
        }
    }
    
    public function delete(){
        $id = I("id",0,'intval');
        
        if(empty($id)){
            $this->error('参数错误');
        }
        
        if(M('FriendLink')->delete($id) !== false){
            $this->success('删除成功');
        }
        
        $this->error('删除失败');
    }
    
    //排序
    public function sort(){
        
        $id = I('sort');
        if(!is_array($id)){
            $this->error('参数错误');
        }
        
        $id = array_map('intval', $id);        
        
        foreach ($id as $k=>$v){
            M('FriendLink')->where(array('id'=>$k))->setField('sort', $v);
        }
        
        $this->success('排序更新成功');
    }
}